using System;
using System.Collections.Generic;
using System.Text;
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace GCalSync.Core
{
    class SyncCheck
    {

        /// <summary>
        /// This will return true if Google is deemed to be the most recent. 
        /// </summary>
        /// <param name="google"></param>
        /// <param name="outlook"></param>
        /// <returns></returns>
        public static bool IsGoogleNewest(EventEntry google, Outlook.AppointmentItem outlook)
        {

            /// This assumes that the outlook entry has all required fields, 
            /// the next check will look for all these fields and if they are
            /// not found then google will be deemed to be the best and the local 
            /// outlook appointment will lose. THIS SHOULD NOT HAPPEN.
            /// 

            /// This is the update time for the AtomEntry
            if (Util.GetCustomField(outlook, "GCalUpdated") == null) return true;
            /// This is the absolute URI for the google entry
            if (Util.GetCustomField(outlook, "GCalID") == null) return true;
            /// This is the local time that the appointment was updated
            if (Util.GetCustomField(outlook, "LocalUpdated") == null) return true;

            // Check the update to see if content has changed. And determine which
            // is newer. If the update stamp is the same then return false (Not Newest)
            if (Util.GetCustomField(outlook, "GCalUpdated") != google.Updated.ToString("s"))
            {
                // Less than zero outlook is less than google. 
                // Or outlook is older than google. So Google is Newest.
                if (DateTime.Compare(DateTime.Parse(Util.GetCustomField(outlook, "GCalUpdated")), google.Updated) < 0)
                {
                    // Google wins, it has been updated
                    return true;
                }
                else
                {
                    // Outlook wins, or is the same, no idea at this point. 
                    return false;
                }
            }
            else
                return false;
        }

        public static bool IsOutlookNewest(EventEntry google, Outlook.AppointmentItem outlook)
        {
            /// If any of the cutom fields are blank, if so then outlook is newest.
            /// 
            /// This is the update time for the AtomEntry
            if (Util.GetCustomField(outlook, "GCalUpdated") == null) return true;
            /// This is the absolute URI for the google entry
            if (Util.GetCustomField(outlook, "GCalID") == null) return true;
            /// This is the local time that the appointment was updated
            if (Util.GetCustomField(outlook, "LocalUpdated") == null) return true;

            // Has already been placed in google and this is not a new object. 
            // Check the update to see if content has changed.
            if (Util.GetCustomField(outlook, "GCalUpdated") != google.Updated.ToString("s"))
            {
                // Less than zero outlook is less than google. 
                // Or outlook is older than google. So Google is Newest.
                if (DateTime.Compare(DateTime.Parse(Util.GetCustomField(outlook, "GCalUpdated")), google.Updated) < 0)
                {
                    // Google wins, it has the most recent update.
                    return false;
                }
                else
                {
                    // Outlook wins, it has the most recent update. (Most likley just the same.)
                    return true;
                }
            }
            else if (Util.GetCustomField(outlook, "LocalUpdated") != null)
            {
                // Check to see if the appointment has been updated locally.
                DateTime localUpdated = DateTime.Parse(Util.GetCustomField(outlook, "LocalUpdated"));
                DateTime lastModified = outlook.LastModificationTime;
                // Get the Sync buffer in Seconds, this deals with the save delay.
                int buffer = GCalSync.Properties.Settings.Default.LocalSyncBuffer;
                long bufferTicks = (buffer * 1000000000) / 100;
                long localUpdatedTicks = localUpdated.Ticks;
                long lastModifiedTicks = lastModified.Ticks;

                // check the local dates for the outlook item to see if they have
                // changed at all. Use the buffer from settings.
                // LocalUpdated is less than LastModificationTime.
                // Update Google. 

                if (Math.Abs(localUpdatedTicks - lastModifiedTicks) > bufferTicks)
                {
                    //force a update.
                    return true;
                }
                else
                    return false;

            }
            else
                return false;

        }

        public static bool IsThereAChange(EventEntry google, Outlook.AppointmentItem outlook)
        {
            /// This is the update time for the AtomEntry
            if (Util.GetCustomField(outlook, "GCalUpdated") == null) return true;
            if (Util.GetCustomField(outlook, "LocalUpdated") == null) return true;

            if (Util.GetCustomField(outlook, "GCalUpdated") != google.Updated.ToString("s"))
                return true;

            // Check to see if the appointment has been updated locally.
            DateTime localUpdated = DateTime.Parse(Util.GetCustomField(outlook, "LocalUpdated"));
            DateTime lastModified = outlook.LastModificationTime;
            // Get the Sync buffer in Seconds, this deals with the save delay.
            int buffer = GCalSync.Properties.Settings.Default.LocalSyncBuffer;
            long bufferTicks = (buffer * 1000000000) / 100;
            long localUpdatedTicks = localUpdated.Ticks;
            long lastModifiedTicks = lastModified.Ticks;

            // check the local dates for the outlook item to see if they have
            // changed at all. Use the buffer from settings.
            // LocalUpdated is less than LastModificationTime.
            // Update Google. 

            if (Math.Abs(localUpdatedTicks - lastModifiedTicks) > bufferTicks)
            {
                //force a update.
                return true;
            }

            return false;


        }




    }
}

